﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class Comments : AdminPage, ICommentsView
    {
        private CommentsPresenter _presenter;

        #region ICommentsView Members

        public void SetComments(IList<Comment> comments)
        {
            rpComments.DataSource = comments;
            rpComments.DataBind();
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Comments.xml"; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new CommentsPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('commentPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }
        
        protected void rpComments_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var comment = (Comment) e.Item.DataItem;
            var litID = (Literal) e.Item.FindControl("litID");
            var litName = (Literal) e.Item.FindControl("litName");

            litID.Text = "<span id=\"D" + comment.ID + "\">";
            litName.Text = comment.Text;
        }
        
        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteComment(int.Parse(hidDeleteCommentID.Value.Substring(1)));
            upnlComments.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initCommentList",
                                                            "initCommentList();", true);
        }
    }
}